package cn.edu.ctbu.api01.helper;

import lombok.Getter;
import lombok.Setter;
import java.util.Map;

@Setter
@Getter
public class Result<T> {
    /**错误代码**/
    private Integer code;
    /**提示信息**/
    private String msg;
    /**具体的内容**/
    private T data;
    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }
    public Result(){
        this.code = 1;
        this.msg = "操作成功";
    }
    public static Result error(){
        return error(-1, "操作失败");
    }
    public static Result error(String msg){
        return error(-1, msg);
    }
    public static Result error(Integer code, String msg){
        Result r = new Result();
        r.setCode(code);
        r.setMsg(msg);
        return r;
    }
    public static Result ok(){
        Result r = new Result();
        return r;
    }
    public static Result ok(String msg){
        Result r = new Result();
        r.setMsg(msg);
        return r;
    }
    public static Result ok(Integer code, String msg, Map<String, Object> map){
        Result r = new Result(code, msg);
        r.setData(map);
        return r;
    }
    public static <T> Result<T> ok(String msg, T data) {
        Result<T> r = new Result<>();
        r.setData(data);
        r.setMsg(msg);
        return r;
    }
}
